package web;

import pojo.SmbmsProvider;
import pojo.SmbmsUser;
import service.SmbmsProviderService;
import service.impl.SmbmsProviderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@WebServlet(value = "/addProviderServlet")

public class AddProviderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        SmbmsProvider smbmsProvider = new SmbmsProvider();
//从界面获取数据
//供应商编号
        String ProviderCode = req.getParameter("providerId");
//供应商名称
        String ProviderName = req.getParameter("providerName");
//联系人
        String ProviderContact = req.getParameter("people");
//电话
        String phone = req.getParameter("phone");
//地址
        String address = req.getParameter("address");
//传真
        String fax = req.getParameter("fax");
//描述
        String describe = req.getParameter("describe");
        smbmsProvider.setProCode(ProviderCode);
        smbmsProvider.setProName(ProviderName);
        smbmsProvider.setProContact(ProviderContact);
        smbmsProvider.setProPhone(phone);
        smbmsProvider.setProAddress(address);
        smbmsProvider.setProFax(fax);
        smbmsProvider.setProDesc(describe);
//创建人, 当前登录人 怎么获取 session
        HttpSession session = req.getSession();
        SmbmsUser providerSession = (SmbmsUser)
                session.getAttribute("userSession");
        smbmsProvider.setCreatedBy(providerSession.getId());
//当前时间
        smbmsProvider.setCreationDate(new Date());
//调用业务层接口
        SmbmsProviderService smbmsProviderService = new SmbmsProviderServiceImpl();
        int i = 0;
        try {
            i = smbmsProviderService.addProvider(smbmsProvider);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (i > 0) {
//添加成功
            req.getRequestDispatcher("/providerServlet").forward(req, resp);
        } else {
//添加失败
            resp.sendRedirect("providerAdd.jsp");
        }

    }
}